package com.gxuwz.attend.web.servlet.instructor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxuwz.attend.context.AppContext;
import com.gxuwz.attend.entity.Classes;

import com.gxuwz.attend.service.instructor.ClassesService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.List;

@WebServlet("/instructor/classes")
public class ClassesServlet extends HttpServlet {

    private ClassesService classesService;
    private final ObjectMapper objectMapper = new ObjectMapper();


    @Override
    public void init() throws ServletException {
        super.init();
        classesService = AppContext.getBean(ClassesService.class);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String id = request.getParameter("id");
        System.out.println(id);
        int page = Integer.parseInt(request.getParameter("page"));
        int size = Integer.parseInt(request.getParameter("size"));
        int offset = (page - 1) * size;

        List<Classes> classesList = classesService.getAllClasses(id, name, size, offset);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        String json = objectMapper.writeValueAsString(classesList);
        response.getWriter().write(json);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Classes classes = objectMapper.readValue(request.getInputStream(), Classes.class);
        classesService.addClass(classes);
        response.setStatus(HttpServletResponse.SC_CREATED); // 201 Created
    }

    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String role = (String) session.getAttribute("userRole");
        if (role.equals("admin")) {
            Classes classes = objectMapper.readValue(request.getInputStream(), Classes.class);
            classesService.updateClass(classes);
            response.setStatus(HttpServletResponse.SC_OK); // 200 OK
        }else {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN); // 返回 403 Forbidden
            response.getWriter().write("你没有权限修改");
        }
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String classID = request.getParameter("classID");
        HttpSession session = request.getSession();
        String role = (String) session.getAttribute("userRole");
        if (role.equals("admin")) {
            classesService.deleteClass(classID);
            response.setStatus(HttpServletResponse.SC_NO_CONTENT); // 204 No Content
        }else {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN); // 403
            response.getWriter().write("你没有权限删除");
        }
    }
}
